'use strict';

const path = require('path');
const assert = require('assert');

const ExcelUtil = require('./util/excel');

class Processor {
  constructor(filePath, name) {
    assert(filePath, 'filePath 不能为空');
    this.filePath = path.resolve(filePath);
    const dir = path.dirname(this.filePath);
    const ext = path.extname(this.filePath);
    const basename = path.basename(this.filePath, ext);
    if (basename.indexOf('[') >= 0 && basename.indexOf(']') >= 0) {
      this.outputFilePath = path.join(dir, `${basename.replace(/\[.*\]/g, '[' + name + ']')}${ext}`);
    } else {
      this.outputFilePath = path.join(dir, `[${name}]${basename}${ext}`);
    }
    assert(dir === path.dirname(this.outputFilePath), `outputFilePath的目录不对,expect:${dir},actual:${path.dirname(this.outputFilePath)}`);
    assert(ext === path.extname(this.outputFilePath), `outputFilePath的扩展名不对,expect:${ext},actual:${path.extname(this.outputFilePath)}`);
  }
  read() {
    return ExcelUtil.readToObjectArray(this.filePath);
  }
  write(data) {
    // console.log(data);
    return ExcelUtil.writeObjectArray(data, this.outputFilePath);
  }
}

module.exports = Processor;
module.exports.ExcelColumns = {
  NEW_COLUMN: '新栏目',
  OLD_COLUMN: '老栏目',
  OLD_COLUMN_NEW: '老栏目(新)',
  OLD_COLUMN_ID: '老栏目Id',
  OLD_COLUMN_SITEID: '老栏目siteId',
  CATEGORY: '类型',
  CATEGORY_ONE: '类型_1',
  TYPE: '文种',
  CLASS: '公文类型',
  LIBRARY: '资源库',
  MAIN: '主/引用',
  NEW_PATH: '新栏目路径',
  NEW_COLUMN_ID: '新栏目Id',
  NEW_COLUMN_CODE: '新栏目code',
  OF_CLASS: 'ofClass',
  OF_TYPE: 'ofType',
  LIBRARIES: 'libraries',
  FILTER: 'Filter',
  FILTER_NEW: 'Filter_New',
  FILTER_NEW_REMARK: 'Filter_New_Remark',
};
